//
//  ViewController.swift
//  Created on 2019/12/10
//  Description <#文件描述#>
//  Copyright © 2019 Huami inc. All rights reserved.
//  @author huangjianwen(huangjianwen@huami.com)    
//

import UIKit

let cell_ID = "cellID"

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
}

// MARK: UICollectionViewDelegate代理
extension ViewController:UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        3
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch section {
        case 0...1:
            return 12
        default:
            return 15
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:FaceCellCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cell_ID, for: indexPath) as! FaceCellCollectionViewCell
        
        switch indexPath.section {
        case 0:
            cell.img.image = UIImage(named: "fan\(indexPath.row)")
        case 1:
            cell.img.image = UIImage(named: "h\(indexPath.row + 1)")
        default:
            cell.img.image = UIImage(named: "y\(indexPath.row + 1)")
        }
        
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader  {
            
            let section = indexPath.section
            var color: UIColor?
            
            let headView: UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "head", for: indexPath)
            for view in headView.subviews {
                view.removeFromSuperview()
            }
            headView.addSubview(setupLabel(section: section, headView: headView))
            switch section {
            case 0:
                color = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
            case 1:
                color = #colorLiteral(red: 1, green: 0.7506214976, blue: 0.3399388492, alpha: 1)
            default:
                color = .green
            }
            headView.backgroundColor = color
            
            return headView
        } else {
            return UICollectionReusableView()
        }
    }
    
    func setupLabel(section: Int, headView: UICollectionReusableView) -> UILabel {
        let label = UILabel()
        label.frame = headView.bounds
        
        switch section {
        case 0:
            label.text = NSLocalizedString("mwft", comment: "美味饭团")
        case 1:
            label.text = NSLocalizedString("kaxg", comment: "可爱小狗")
        default:
            label.text = NSLocalizedString("kxyl", comment: "酷炫饮料")
        }
        
        label.textColor = .black
        label.textAlignment = .center
        
        return label
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let textToShare = "fortune"
        var imgStr = ""
        switch indexPath.section {
        case 0:
            imgStr = "fan\(indexPath.row)"
            case 1:
            imgStr = "h\(indexPath.row + 1)"
            case 2:
            imgStr = "y\(indexPath.row + 1)"
        default:
            imgStr = "fan0"
        }
        let imageToShare = UIImage(named: imgStr)
//
//        let vc = UIViewController()
//        vc.view.frame = view.bounds
//        view.addSubview(vc.view)
//        self.addChild(vc)
        zq_share(items: [textToShare, imageToShare!]) { (success, msg) in
            print("\(success ? "成功" : "失败")")
        }
    }
}

extension ViewController {
    private func setupUI() {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: view.bounds.width / 3 - 10, height: view.bounds.width / 3)
        
        flowLayout.scrollDirection = .vertical
        flowLayout.headerReferenceSize = CGSize(width: view.bounds.width, height: 100)
        let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: flowLayout)
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.register(UINib(nibName: "FaceCellCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cell_ID)
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "head")
        collectionView.backgroundColor = .white
        view.addSubview(collectionView)
    }
    
    /// 分享方法
    func zq_share(items: [Any], completion:(_ successed: Bool, _ message: String)->()){
        
        if items.count == 0 {
            return
        }
        
        let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
   
        activityVC.excludedActivityTypes = []

        activityVC.completionWithItemsHandler = {
            (type, flag, array, error) -> Swift.Void in
            print(type ?? "")
        }

        present(activityVC , animated: true, completion: nil)
    }
}




